﻿using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarLine.Helpers
{
    internal static class MathHelper
    {
        ///<summary>获得直线表达式 格式是 ax + by + c = 0。</summary>
        public static void GetLineFormula(float x1, float y1, float x2, float y2, out float a, out float b, out float c)
        {
            a = 0;
            b = 0;
            c = 0;
            var dx = x2 - x1; // x的差
            var dy = y2 - y1; // y的差
            c = dx * y1 - dy * x1;
            b = -dx;
            a = dy;
        }

        public static float GetLineFormatValue(float a, float b, float c, float x)
        {
            if (b == 0) return x;
            var alpha = -a / b;
            var cons = -c / b;
            return alpha * x + cons;
        }

        // Q 是垂足，获得两点之间的垂足。
        public static Vector2 GetQ(float ax, float ay, float bx, float by, float px, float py)
        {
            // 直线AB的斜率。
            var mx = bx - ax;
            var my = by - ay;

            var ml = my / mx;
            var rml = mx / my;

            var qx = (rml * px + py + ml * ax - ay) / (ml + rml);
            var qy = ml * (qx - ax) + ay;
            return new Vector2(qx, qy);
        }

    }
}
